import java.util.*;


  class TreeNode {
   int val = 0;
   TreeNode left = null;
   TreeNode right = null;
   public TreeNode(int val) {
     this.val = val;
   }
 }


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */

    public static int[] pruneSequence (TreeNode root) {
        // write code here
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();

        while (root != null){
            ArrayList<Integer> res = new ArrayList<>();
            fun(root,res);
            list.add(res);
        }
        int n = 0;
        for(int i = 0;i<list.size();i++){
            n += list.get(i).size();
        }
        int[] ret = new int[n];
        for(int i = 0;i<n;){
            for(int j = list.size()-1;j>=0;j--){
                for(int k = 0;k<list.get(j).size();k++){
                    ret[i] = list.get(j).get(k);
                    i++;
                }
            }
        }
        return null;
    }
    public static void fun(TreeNode root, ArrayList<Integer> res){
        if(root == null){
            return;
        }
        if(root.left == null && root.right == null){
            res.add(root.val);
            return;
        }
        if(root.left != null && root.left.left == null && root.left.right == null){
            res.add(root.left.val);
            root.left = null;
        }
        if(root.right != null && root.right.left == null & root.right.right == null){
            res.add(root.right.val);
            root.right = null;
        }
        fun(root.left,res);
        fun(root.right,res);
//        if(root.left == null && root.right == null){
//            res.add(root.val);
//            root.val = 0;
//        }
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        a.left = new TreeNode(2);
        a.left.left = new TreeNode(4);
        a.left.right = new TreeNode(5);
        a.left.left.right = new TreeNode(7);
        a.right = new TreeNode(3);
        a.right.right = new TreeNode(6);
        pruneSequence(a);
    }
}